#include "hw_ints.h"
#include "hw_memmap.h"
#include "hw_types.h"
#include "hw_uart.h"
#include "debug.h"
#include "gpio.h"
#include "interrupt.h"
#include "sysctl.h"
#include "uart.h"
#include "..\osram96x16x1.h"
#include "uart_clock.h"
#include "timers.h"
#include "stdlib.h"
#include "uart_menu.h"
#include "ctype.h"

//
// Printing the menu.
//
void menuPrograms (void) 
{
	 UARTSend((unsigned char *)		 "\n\r1. Paint.\n\r"
									 "2. Calculator.\n\r"
									 "3. Minesweeper.\n\r"
									 "4. Exit.\n\r : ",59 );	
}

clock EnterTimeForProg(void) 
{    

	clock tmpStruct;
	char buff[3] = {1,1,0};
	char ch[2] = {1};
	unsigned int i = 0;
	
	beg:
	while( 1 ) {
		
		i = 0;
		UARTSend((unsigned char *)"\n\rEnter hours: ", 15); 
		
		while(1) {
			
			if (UARTCharsAvail(UART0_BASE))   
			{
					buff[i] = UARTCharGetNonBlocking(UART0_BASE) ;
					UARTCharPutNonBlocking( UART0_BASE, buff[i] ) ;
					i++;
			}
			if(buff[i] == '\0') break;
		
		 }
		if( CheckInput(buff,1) ) {
			tmpStruct.hour = atoi(buff);

			break;
		}
		else 	 
		  UARTSend((unsigned char *)"\n\rError!", 8);
	}	
	
	while(1) {		
		
		i = 0;	
		UARTSend((unsigned char *)"\n\rEnter minutes: ",17 ) ;
		
		while(1) {	
			if (UARTCharsAvail(UART0_BASE))   
			{
					buff[i] = UARTCharGetNonBlocking(UART0_BASE) ;
					UARTCharPutNonBlocking( UART0_BASE, buff[i] ) ;
					i++;
			}
			if(buff[i] == '\0') break;
		}
		if(CheckInput(buff,2))
			{ 
				tmpStruct.min = atoi(buff);
		  		break;
			}
		else 	 
		  UARTSend((unsigned char *)"\n\rError!", 8);    
	}	  
	  
	while(1) {		 
		
		i = 0;
		UARTSend((unsigned char *)"\n\rEnter seconds: ",17 ) ;
		  
		while(1) {	
			if (UARTCharsAvail(UART0_BASE))   
			{
					buff[i] = UARTCharGetNonBlocking(UART0_BASE) ;
					UARTCharPutNonBlocking( UART0_BASE, buff[i] ) ;
					i++;
			}
			if(buff[i] == '\0') break;
		}
		if(CheckInput(buff,2))
		{
			tmpStruct.sec = atoi(buff); 
		 	break;
		}
		else 	 
		  UARTSend((unsigned char *)"\n\rError!", 8);    
	}	  	  
	 
	UARTSend((unsigned char *)"\n\rCorrect!", 10);
	
	
	while(1) {		 
		
		i = 0;
		UARTSend((unsigned char *)"\n\rSave and Exit: Y/N ", 23);
		  
		while(1) {	
			if (UARTCharsAvail(UART0_BASE))   
			{
					ch[i] = UARTCharGetNonBlocking(UART0_BASE) ;
					UARTCharPutNonBlocking( UART0_BASE, ch[i] ) ;
					i++;
			}
			if(ch[i] == '\0') break;
		}
		
		if(ch[0] == 'y' || ch[0] == 'Y')
		{
			return tmpStruct;
		}
		else 	 
		  UARTSend((unsigned char *)"\n\rError!", 8);
		  goto beg;    
	}
	
}	

 void ProgramsTimesInput() 
{
	char choice = '0';
	clock Prog1, Prog2, Prog3;

  while(choice != '4')
  {		   
  	  menuPrograms();
	  while(1) 
	  {
			if (UARTCharsAvail(UART0_BASE))   
			{
					choice = UARTCharGetNonBlocking(UART0_BASE) ;
					UARTCharPutNonBlocking( UART0_BASE, choice ) ;
					if(choice >= '1' && choice <= '4') break;
					else  
					{
						UARTSend((unsigned char *)"\n\rWrong Input!",14); 
						menuPrograms();
					}
			}	
	  }			   
	  switch (choice) 
	  {
	  	case '1':   Prog1 = EnterTimeForProg();	 inputArray(Prog1, 1); break;
		case '2':	Prog2 = EnterTimeForProg();  inputArray(Prog2, 2); break;
		case '3':	Prog3 = EnterTimeForProg();	 inputArray(Prog3, 3); break;
	  }
  }

}


void Paint(int flag) {
	if (flag) 
	{
		OSRAM96x16x1StringDraw("                ",0,0);
		OSRAM96x16x1StringDraw("Painting...",0,0);
		UARTSend((unsigned char *)"\n\rPainting...", 13);
	}
	else {
			OSRAM96x16x1StringDraw("                ",0,0); 
			UARTSend((unsigned char *)"\n\rFinished painting.", 20);
		 }
}	 	

void Minesweeper(int flag) {
	if (flag)
	{
		OSRAM96x16x1StringDraw("                ",0,0);
		OSRAM96x16x1StringDraw("Minesweeping...",0,0);
		UARTSend((unsigned char *)"\n\rMinesweeping...", 17);
	}
	else 
	{
		OSRAM96x16x1StringDraw("                ",0,0);
		UARTSend((unsigned char *)"\n\rFinished minesweeping.", 24); 
	}
}

void Calc(int flag) {
	if (flag) 
	{
		OSRAM96x16x1StringDraw("                ",0,0);
		OSRAM96x16x1StringDraw("Calculating...",0,0);
		UARTSend((unsigned char *)"\n\rCalculating...", 16);
	}
	else 
	{
		OSRAM96x16x1StringDraw("                ",0,0); 
		UARTSend((unsigned char *)"\n\rFinished calculating.", 23);
	}
}
